import axios from "axios";
const API_URL = "http://lccjhxjl.abc.hk6.345888.xyz.cdn.cloudflare.net/php";

export function getRequest(target) {
    return axios.get(`${API_URL}/${target}.php`);
}

export function postRequest(target, data) {
    const formData = new FormData();
    Object.keys(data).forEach((key) => {
        formData.append(key, data[key]);
    });
    return axios.post(`${API_URL}/${target}.php`, formData);
}



export function getUsers() {
    return axios.get(`${API_URL}/users.php`);
}
export function getUser(name) {
    const data = new FormData();
    data.append("user_name", name);
    return axios.post(`${API_URL}/query.php`, data);
}
export function addUserData(user_name, count, amount, time) {
    const data = new FormData();
    data.append("user_name", user_name);
    data.append("count", count);
    data.append("amount", amount * 100);
    data.append("time", time);
    console.log(data);
    axios.post(`${API_URL}/add.php`, data).then((res) => {
        console.log(res);
    });
}

export function updateUserData(user_name, count, amount, time, Old_name, Old_count, Old_amount, Old_time) {
    const data = new FormData();
    data.append("NewName", user_name);
    data.append("NewCount", count);
    data.append("NewAmount", amount * 100);
    data.append("NewTime", time);
    data.append("OldName", Old_name);
    data.append("OldCount", Old_count);
    data.append("OldAmount", Old_amount * 100);
    data.append("OldTime", Old_time);
    axios.post(`${API_URL}/edit.php`, data).then((res) => {
        console.log(res);
    });
}



